<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseAdminController;
use App\Lib\Enum\ValRule;
use App\Lib\Logger;
use App\Lib\Validates;
use App\Models\Admin\Role;
use App\Rules\CheckRule;
use App\Service\AuthService;
use Illuminate\Http\Request;

class RoleController extends BaseAdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $roles = (new AuthService())->getAll($request->all());
        //结果处理
        $roles = !empty($roles)?$this->success($roles):$this->errorArray(80000, "没有角色");
        return view("admin.role.index",['roles'=>$roles["data"],'error'=>$roles["error"]]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $params = (new CheckRule($request->all("data")["data"],ValRule::NAME,ValRule::NAME_MSG))->check();
        if($params["error"]>0){
            return $params;
        }
        //添加
        $result = (new AuthService())->create($params["data"],getCurrentSiteId());
        if(is_array($result)&&array_key_exists("error",$result)){
            return $this->error($result["error"], $result["msg"]);
        }
        if(!empty($result)){
            $this->log(Logger::LOG_LEVEL_INFO,Logger::ROLE,'store','添加成功',$result);
            return $this->success($result, "添加成功");
        }
        $this->log(Logger::LOG_LEVEL_ERROR,Logger::ROLE,'store','添加失败',$params);
        return $this->error(70000, "添加失败");
    }

    /**
     * Display the specified resource.
     *
     * @param Role $role
     * @return \Illuminate\Http\Response
     */
    public function show(Role $role) {
        $result = (new AuthService())->getInfo($role);
        return view("admin.role.show",["role"=>$result]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Role $role
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        //验证
        $check = Validates::isPositiveInteger($id);
        if(!$check){
            return $this->tempNotice("admin.role.index","id必须是正整数","error");
        }
        $list = (new AuthService())->getOne($id);
        //结果处理
        $result = !empty($list)?$this->success($list):$this->error(80000, "没有角色");
        return view("admin.role.edite",["role"=>$result["data"],"error"=>$result["error"]]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id) {
        $params = (new CheckRule($request->all("data")["data"],ValRule::NAME,ValRule::NAME_MSG))->check();
        if($params["error"]>0){
            return $params;
        }
        $check = Validates::isPositiveInteger($id);
        if(!$check){
            return $this->tempNotice("admin.role.index","id必须是正整数","error");
        }
        //修改
        $result = (new AuthService())->update($params["data"],$id);
        if(!$result){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ROLE,'update','不可修改系统角色，或角色修改失败',$params);
            return $this->error( 80000, "系统角色不允许修改");
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ROLE,'update','更新成功',$params);
        return $this->success("","更新成功");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //验证
        $check = Validates::isPositiveInteger($id);
        if(!$check){
            return $this->tempNotice("admin.role.index","id必须是正整数","error");
        }
        //删除
        $result = (new AuthService())->delete($id);
        if(!$result){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ROLE,'destroy','系统角色不可删除，或删除失败',['del_id'=>$id]);
            return $this->error( 80001, "系统角色不可删除");
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ROLE,'destroy','删除成功',['del_id'=>$id]);
        return $this->layuiDataSuccess("",0,"删除成功");
    }

    /**
     * 分配权限列表
     */
    public function per_form($id){
        //角色
        $role = (new AuthService())->getOne($id)->toArray();
        if(empty($role)){
            return $this->tempNotice("admin.role.index","角色不存在","error");
        }
        //权限
        $scope = (new AuthService())->per_form($role["guard_name"]);

        if(array_key_exists("error",$scope)&&$scope["error"]>0){
            return $this->tempNotice("admin.role.index",$scope["msg"],"error");
        }
        //角色已有权限
        $permission=(new AuthService())->has_permission($id);


        $role_permission = !empty($permission)?$this->success($permission):$this->error(80000, "没有权限");
        return view("admin.role.ass_per_form",["role_id"=>$id,"role_permission"=>$role_permission,"permissionGroups"=>$scope,"role_name"=>$role["name"]]);
    }

    /**
     * 分配权限
     */
    public function assign(Request $request,$role_id){
        //验证
        $check = Validates::isPositiveInteger($role_id);
        if(!$check){
            return $this->tempNotice("admin.role.index","id必须是正整数","error");
        }
        $val = (new CheckRule($request->all(),ValRule::ASSIGN_PERMISSION,ValRule::ASSIGN_PERMISSION_MSG))->check();
        if($val["error"]>0){
            return $this->tempNotice("admin.role.permission_assign",$val["msg"],"error");
        }
        //分配
        $result = (new AuthService())->assign_permission($role_id,$val['data']['permission']);
        if(is_array($result)&&array_key_exists("error",$result)){
            return $this->error($result["error"], $result["msg"]);
        }
        if(!$result){
            $this->log(Logger::LOG_LEVEL_ERROR,Logger::ROLE,'assign','权限赋予失败',['role_id'=>$role_id]);
            return $this->error( 200000, "权限赋予失败");
        }
        $this->log(Logger::LOG_LEVEL_INFO,Logger::ROLE,'assign','删除成功',['role_id'=>$role_id]);
        return $this->success("","权限赋予成功");
    }
}
